package com.haixiaoke.saas.house.service.impl;

import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.house.domain.ChargeItemDetail;
import com.haixiaoke.saas.house.mapper.ChargeItemDetailMapper;
import com.haixiaoke.saas.house.service.IChargeItemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 房屋收费项目明细Service业务层处理
 *
 * @author xiaolong
 * @date 2023-11-25
 */
@Service
public class ChargeItemDetailServiceImpl implements IChargeItemDetailService {
    @Autowired
    private ChargeItemDetailMapper chargeItemDetailMapper;

    /**
     * 查询房屋收费项目明细
     *
     * @param chargeItemDetailId 房屋收费项目明细主键
     * @return 房屋收费项目明细
     */
    @Override
    public ChargeItemDetail selectChargeItemDetailByChargeItemDetailId(Long chargeItemDetailId) {
        return chargeItemDetailMapper.selectChargeItemDetailByChargeItemDetailId(chargeItemDetailId);
    }

    /**
     * 查询房屋收费项目明细列表
     *
     * @param chargeItemDetail 房屋收费项目明细
     * @return 房屋收费项目明细
     */
    @Override
    public List<ChargeItemDetail> selectChargeItemDetailList(ChargeItemDetail chargeItemDetail) {
        return chargeItemDetailMapper.selectChargeItemDetailList(chargeItemDetail);
    }

    /**
     * 新增房屋收费项目明细
     *
     * @param chargeItemDetail 房屋收费项目明细
     * @return 结果
     */
    @Override
    public int insertChargeItemDetail(ChargeItemDetail chargeItemDetail) {
        chargeItemDetail.setCreateTime(DateUtils.getNowDate());
        return chargeItemDetailMapper.insertChargeItemDetail(chargeItemDetail);
    }

    /**
     * 修改房屋收费项目明细
     *
     * @param chargeItemDetail 房屋收费项目明细
     * @return 结果
     */
    @Override
    public int updateChargeItemDetail(ChargeItemDetail chargeItemDetail) {
        ChargeItemDetail byChargeItemDetailId = chargeItemDetailMapper.selectChargeItemDetailByChargeItemDetailId(chargeItemDetail.getChargeItemDetailId());
        if (!TenantContext.getTenantId().equals(byChargeItemDetailId.getTenantId())) {
            return 0;
        }


        chargeItemDetail.setUpdateTime(DateUtils.getNowDate());
        return chargeItemDetailMapper.updateChargeItemDetail(chargeItemDetail);
    }

    /**
     * 批量删除房屋收费项目明细
     *
     * @param chargeItemDetailIds 需要删除的房屋收费项目明细主键
     * @return 结果
     */
    @Override
    public int deleteChargeItemDetailByChargeItemDetailIds(Long[] chargeItemDetailIds) {
        ChargeItemDetail chargeItemDetail = new ChargeItemDetail();
        chargeItemDetail.setTenantId(TenantContext.getTenantId());
        chargeItemDetail.setChargeItemDetailIds(chargeItemDetailIds);
        List<ChargeItemDetail> chargeItemDetailList = chargeItemDetailMapper.selectChargeItemDetailByChargeItemDetailIds(chargeItemDetail);
        if (chargeItemDetailList.size() < chargeItemDetailIds.length) {
            return 0;
        }

        return chargeItemDetailMapper.deleteChargeItemDetailByChargeItemDetailIds(chargeItemDetailIds);
    }

    /**
     * 删除房屋收费项目明细信息
     *
     * @param chargeItemDetailId 房屋收费项目明细主键
     * @return 结果
     */
    @Override
    public int deleteChargeItemDetailByChargeItemDetailId(Long chargeItemDetailId) {
        return chargeItemDetailMapper.deleteChargeItemDetailByChargeItemDetailId(chargeItemDetailId);
    }
}
